package com.chapal.ib.client;

import java.util.HashMap;

import org.apache.log4j.Logger;

import com.chapal.ib.activemq.client.Producer;
import com.chapal.ib.model.HistoricalData;
import com.chapal.ib.util.xml.XmlUtil;
import com.ib.client.Contract;
import com.ib.client.ContractDetails;
import com.ib.client.EWrapper;
import com.ib.client.Execution;
import com.ib.client.Order;
import com.ib.client.OrderState;

public class EWrapperImpl implements EWrapper {
	static final Logger logger = Logger.getLogger(EWrapperImpl.class);

	HashMap stockMap = null;
	Producer historicalProducer = null;
	Producer tickDataProducer = null;
	Producer accountValueProducer = null;

	@Override
	public void bondContractDetails(int arg0, ContractDetails arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void contractDetails(int arg0, ContractDetails arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void contractDetailsEnd(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void currentTime(long arg0) {

	}

	@Override
	public void execDetails(int arg0, Contract arg1, Execution arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void fundamentalData(int arg0, String arg1) {
		// TODO Auto-generated method stub

	}

	// void historicalData(int reqId, String date, double open, double high,
	// double low, double close, int volume, int count, double WAP, boolean
	// hasGaps);
	@Override
	public void historicalData(int reqId, String date, double open, double high, double low, double close, int volume, int count, double WAP, boolean hasGaps) {
		EWrapperImpl.logger.debug("Company" + this.stockMap.get(reqId) + "date" + date + "open" + open + "high" + high + "low" + low + "close" + close + "volume" + volume + "count" + count + "WAP"
				+ WAP + "hasGaps" + hasGaps);
		System.out.println("Company" + this.stockMap.get(reqId) + "date" + date + "open" + open + "high" + high + "low" + low + "close" + close + "volume" + volume + "count" + count + "WAP" + WAP
				+ "hasGaps" + hasGaps);
		final HistoricalData hdata = new HistoricalData();
		hdata.setName((String) this.stockMap.get(Integer.toString(reqId)));
		hdata.setDate(date);
		hdata.setPrice(WAP);
		try {
			this.historicalProducer.sendMessage(hdata);
		} catch (final Exception e) {
			// TODO Auto-generated catch block
			EWrapperImpl.logger.error("", e);

			e.printStackTrace();
		}
	}

	@Override
	public void managedAccounts(String arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void nextValidId(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void openOrder(int arg0, Contract arg1, Order arg2, OrderState arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	public void orderStatus(int arg0, String arg1, int arg2, int arg3, double arg4, int arg5, int arg6, double arg7, int arg8, String arg9) {
		// TODO Auto-generated method stub

	}

	@Override
	public void realtimeBar(int arg0, long arg1, double arg2, double arg3, double arg4, double arg5, long arg6, double arg7, int arg8) {
		// TODO Auto-generated method stub

	}

	@Override
	public void receiveFA(int arg0, String arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void scannerData(int arg0, int arg1, ContractDetails arg2, String arg3, String arg4, String arg5, String arg6) {
		// TODO Auto-generated method stub

	}

	@Override
	public void scannerDataEnd(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void scannerParameters(String arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void tickEFP(int arg0, int arg1, double arg2, String arg3, double arg4, int arg5, String arg6, double arg7, double arg8) {
		// TODO Auto-generated method stub

	}

	@Override
	public void tickGeneric(int arg0, int arg1, double arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void tickOptionComputation(int arg0, int arg1, double arg2, double arg3, double arg4, double arg5) {
		// TODO Auto-generated method stub

	}

	public HashMap getStockMap() {
		return this.stockMap;
	}

	public void setStockMap(HashMap stockMap) {
		this.stockMap = stockMap;
	}

	@Override
	public void tickPrice(int arg0, int arg1, double arg2, int arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	public void tickSize(int arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void tickString(int arg0, int arg1, String arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateAccountTime(String arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateAccountValue(String arg0, String arg1, String arg2, String arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateMktDepth(int arg0, int arg1, int arg2, int arg3, double arg4, int arg5) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateMktDepthL2(int arg0, int arg1, String arg2, int arg3, int arg4, double arg5, int arg6) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateNewsBulletin(int arg0, int arg1, String arg2, String arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updatePortfolio(Contract arg0, int arg1, double arg2, double arg3, double arg4, double arg5, double arg6, String arg7) {
		// TODO Auto-generated method stub

	}

	@Override
	public void connectionClosed() {
		// TODO Auto-generated method stub

	}

	@Override
	public void error(Exception arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void error(String error) {
		EWrapperImpl.logger.debug("Error from TWS.IB" + error);

	}

	@Override
	public void error(int arg0, int arg1, String error) {
		EWrapperImpl.logger.debug("Error from TWS.IB" + arg0 + arg1 + error);
	}

	public Producer getHistoricalProducer() {
		return this.historicalProducer;
	}

	public void setHistoricalProducer(Producer historicalProducer) {
		this.historicalProducer = historicalProducer;
	}

	public Producer getTickDataProducer() {
		return this.tickDataProducer;
	}

	public void setTickDataProducer(Producer tickDataProducer) {
		this.tickDataProducer = tickDataProducer;
	}

	public Producer getAccountValueProducer() {
		return this.accountValueProducer;
	}

	public void setAccountValueProducer(Producer accountValueProducer) {
		this.accountValueProducer = accountValueProducer;
	}

	public void init() throws Exception {
		if ((this.tickDataProducer == null) || (this.accountValueProducer == null) || (this.historicalProducer == null) || (this.stockMap == null)) throw new Exception(
				"Unable to initialize EWrapperImpl.Producer  or stockMap  is null");
		EWrapperImpl.logger.debug("initializing with" + XmlUtil.toXML(this.stockMap));
	}

}
